package admin

import (
	"ginshop02/models"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

type ManagerController struct {
	BaseController
}

func (con ManagerController) Index(c *gin.Context) {
	var managerList []models.Manager
	models.DB.Preload("Role").Find(&managerList)
	c.HTML(http.StatusOK, "admin/manager/index.html", gin.H{
		"managerList": managerList,
	})

}
func (con ManagerController) Add(c *gin.Context) {
	var roleList []models.Role
	models.DB.Find(&roleList)
	c.HTML(http.StatusOK, "admin/manager/add.html", gin.H{
		"roleList": roleList,
	})
}
func (con ManagerController) DoAdd(c *gin.Context) {
	roleId := models.Int(c.PostForm("role_id"))
	if roleId == 0 {
		con.error(c, "传入数据错误", "/admin/manager/add")
		return
	}
	username := strings.Trim(c.PostForm("username"), " ")
	password := strings.Trim(c.PostForm("password"), " ")
	email := strings.Trim(c.PostForm("email"), " ")
	mobile := strings.Trim(c.PostForm("mobile"), " ")
	//用户名和密码长度是否合法
	if len(username) < 2 || len(password) < 6 {
		con.error(c, "用户名或者密码的长度不合法", "/admin/manager/add")
		return
	}
	//用户名是否重复
	var managerList []models.Manager
	models.DB.Where("username = ?", username).First(&managerList)
	if len(managerList) > 0 {
		con.error(c, "用户名已经存在", "/admin/manager/add")
		return
	}

	manager := models.Manager{
		Username: username,
		Password: models.Md5(password),
		Mobile:   mobile,
		Email:    email,
		Status:   1,
		RoleId:   roleId,
		AddTime:  int(models.GetUnix()),
	}
	err := models.DB.Create(&manager).Error
	if err != nil {
		con.error(c, "添加失败", "/admin/manager/add")
	} else {
		con.success(c, "添加成功", "/admin/manager")
	}
}
func (con ManagerController) Edit(c *gin.Context) {
	id := models.Int(c.Query("id"))
	if id == 0 {
		con.error(c, "传入数据错误", "/admin/manager")
		return
	}
	var manager models.Manager
	manager.Id = id
	models.DB.Find(&manager)
	// 获取所有角色
	var roleList []models.Role
	models.DB.Find(&roleList)
	c.HTML(http.StatusOK, "admin/manager/edit.html", gin.H{
		"manager":  manager,
		"roleList": roleList,
	})

}
func (con ManagerController) DoEdit(c *gin.Context) {
	id := models.Int(c.PostForm("id"))
	if id == 0 {
		con.error(c, "传入数据错误", "/admin/manager")
		return
	}
	roleId := models.Int(c.PostForm("role_id"))
	if roleId == 0 {
		con.error(c, "传入数据错误", "/admin/manager")
		return
	}
	username := strings.Trim(c.PostForm("username"), " ")
	password := strings.Trim(c.PostForm("password"), " ")
	email := strings.Trim(c.PostForm("email"), " ")
	mobile := strings.Trim(c.PostForm("mobile"), " ")
	// 执行修改
	manager := models.Manager{Id: id}
	models.DB.Find(manager)
	manager.Username = username
	manager.Email = email
	manager.Mobile = mobile
	manager.RoleId = roleId
	if password != "" {
		manager.Password = models.Md5(password)
	}
	err := models.DB.Save(&manager).Error
	if err != nil {
		con.error(c, "修改失败", "/admin/manager")
	} else {
		con.success(c, "修改成功", "/admin/manager")
	}

}

func (con ManagerController) Delete(c *gin.Context) {
	c.String(http.StatusOK, "-add--文章-")
}
